#define BE_DLL __declspec(dllexport)

#include <Scene.h>

Scene::Scene()
{

}

Scene::~Scene()
{

}

void Scene::FreeObjects( void )
{
	for( unsigned int i = 0; i < Objects.size(); i++ )
		Objects[i]->FreeObj();

	for( unsigned int i = 0; i < Textures.size(); i++ )
		SDL_FreeSurface( Textures[i]->surface );

	Objects.clear();
	Textures.clear();
}

void Scene::Render( SDL_Surface *surf )
{
	for( unsigned int i = 0; i < Objects.size(); i++ )
		Objects[i]->Blit( surf );
}

void Scene::AddObject( char *file, int posX, int posY )
{
	Object *tmp = NULL;
	int TexFound = -1;

	if( file )
	{
		tmp = new Object;

		TexFound = VerifTex( file );

		if( TexFound > -1 )
			tmp->SetSurface( Textures[TexFound]->surface );
		else
			tmp->SetSurface( AddTexture( file ) );

		tmp->SetPos( posX, posY );
		Objects.push_back( tmp );
	}
}

SDL_Surface *Scene::AddTexture( char *file )
{
	if(file)
	{
		Texture *tmp = NULL;

		tmp = new Texture;

		printf( "Load texture : %s\n", file );

		tmp->dest = file;
		tmp->surface = IMG_Load( file );

		if( !tmp->surface )
		{
			printf( "Can't load %s : %s\n", file, SDL_GetError() );

			return NULL;
		}

		Textures.push_back( tmp );

		return tmp->surface;
	}

	return NULL;
}

int Scene::VerifTex( char *tex )
{
	if( tex )
		for( int i = 0; i < (int)Textures.size(); i++)
				if( !strcmp( Textures[i]->dest, tex ) )
					return i;

	return -1;
}